package servlet;

import org.dom4j.DocumentException;
import util.CheckUtil;
import util.MessageUtill;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Map;

import static util.MessageUtill.MESSAGE_EVENT;

/**
 * Created by zzz on 2016/11/23.
 */
public class WeiXinServlet extends HttpServlet {


    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //开发者提交信息后，微信服务器将发送GET请求到填写的服务器地址URL上，GET请求携带参数如下表所示
        String signature = req.getParameter("signature"); //微信加密签名，signature结合了开发者填写的token参数和请求中的timestamp参数、nonce参数。
        String timestamp = req.getParameter("timestamp");   // 时间戳
        String nonce = req.getParameter("nonce");           //随机数
        String echostr = req.getParameter("echostr");       //随机字符串

        //若确认此次GET请求来自微信服务器，请原样返回echostr参数内容，则接入生效，成为开发者成功，否则接入失败。
        PrintWriter out = resp.getWriter();
        if (CheckUtil.checkSignature(signature, timestamp, nonce)) {
            out.print(echostr);
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");

        PrintWriter out = resp.getWriter();  //获得输出流对象
        //接收的xml文件转换成集合类型
        try {
            Map<String, String> map = MessageUtill.xmlToMap(req);
            String fromUserName = map.get("FromUserName");  // 开发者微信号
            String toUserName = map.get("ToUserName");      //接收方帐号（收到的OpenID）
            String msgType = map.get("MsgType");            //消息类型 text
            String content = map.get("Content");            //文本消息内容

            String message = null;
            if (MessageUtill.MESSAGE_TEXT.equals(msgType)) {
                if ("1".equals(content)) {
                    message = MessageUtill.initText(toUserName, fromUserName, MessageUtill.firstMenu());
                } else if ("2".equals(content)) {
                    message = MessageUtill.initNewsMessage(toUserName, fromUserName);
                } else if ("3".equals(content)) {
                    message = MessageUtill.initImageMessage(toUserName, fromUserName);
                } else if ("4".equals(content)) {
                    message = MessageUtill.initMusicMessage(toUserName, fromUserName);
                } else if ("?".equals(content) || "？".equals(content)) {
                    message = MessageUtill.initText(toUserName, fromUserName, MessageUtill.menuText());
                }
            } else if (MESSAGE_EVENT.equals(msgType)) {
                String eventType = map.get("Event"); //获取到事件类型
                if (MessageUtill.MESSAGE_SUBSCRIBE.equals(eventType)) {  // 获取具体的事件类型
                    message = MessageUtill.initText(toUserName, fromUserName, MessageUtill.menuText());
                }else if(MessageUtill.MESSAGE_CLICK.equals(eventType)){
                    message = MessageUtill.initText(toUserName, fromUserName, MessageUtill.menuText());
                }else if (MessageUtill.MESSAGE_VIEW.equals(eventType)){
                    String url = map.get("EventKey");
                    message = MessageUtill.initText(toUserName, fromUserName, url);
                }else if (MessageUtill.MESSAGE_SCANCODE.equals(eventType)){
                    String key = map.get("EventKey");
                    message = MessageUtill.initText(toUserName, fromUserName, key);
                }
            }else if (MessageUtill.MESSAGE_LOCATION.equals(msgType)){
                String label = map.get("Label");
                message = MessageUtill.initText(toUserName, fromUserName, label);
            }
            out.print(message);  //写出到微信客户端。
        } catch (DocumentException e) {
            e.printStackTrace();
        } finally {
            out.close();
        }


    }
}
